package com.barunsw.bwf.remote.corba;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.omg.CORBA.ORB;

public class IorTextOrbSessionFactory implements OrbSessionFactory {
	public static Map<String, OrbSession> sessionMap = new HashMap<String, OrbSession>();

	private OrbSource orbSource;
	private String iorText;
	
	@Override
	public void setOrbSource(OrbSource orbSource) {
		this.orbSource = orbSource;
	}
	
	public void setIorText(String iorText) {
		this.iorText = iorText;
	}
	
	@Override
	public OrbSession getSession() {
		OrbSession orbSession = null;
		
		if ( sessionMap.containsKey(iorText) ) {
			orbSession = sessionMap.get(iorText);
		}
		else {
			Properties props = System.getProperties();
			props.put("org.omg.CORBA.ORBClass", orbSource.getOrbClassName());
			props.put("org.omg.CORBA.ORBSingletonClass", orbSource.getOrbSingletonClassName());

			ORB orb = ORB.init((String[])null, props);

			org.omg.CORBA.Object obj = orb.string_to_object(iorText);

			orbSession = new BasicOrbSession();
			orbSession.setORB(orb);
			orbSession.setObject(obj);
			
			sessionMap.put(iorText, orbSession);
		}
		
		return orbSession;
	}
}